package cn.gailvlun.gll.presentation.chat;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityChatSettingBinding;
import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.vm.ChatSettingViewModel;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.Observable;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class ChatSettingActivity extends BaseActivity {

    public static Observable<Result<Activity>> openActivity(Activity context, int profileId) {
        Intent intent = new Intent(context, ChatSettingActivity.class);
        intent.putExtra("profileId", profileId);

        return RxActivityResult.on(context)
                .startIntent(intent);
    }

    private ActivityChatSettingBinding mBinding;

    private ChatSettingViewModel mViewModel;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_chat_setting);
        mBinding.setPresenter(new Presenter());
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        
        int profileId = getIntent().getIntExtra("profileId", -1);

        mViewModel = ViewModelProviders.of(this).get(ChatSettingViewModel.class);
        Profile profile = mViewModel.getProfile(profileId);
        mBinding.setItem(profile);
    }

    public class Presenter {
        public void onDeleteMessage(final Profile item) {
            
            DialogUtil.showWarning(mContext, "是否清空所有消息？", new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        mViewModel.deleteAllMessage("user:" + item.getId());
                        ToastUtil.showShort("已清空");
                        setResult(RESULT_OK);
                    }
                }
            });
        }
    }
}
